package br.com.comanda;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import br.com.comanda.customadapters.BoardAdapter;
import br.com.comanda.service.AccountService;
import br.com.comanda.service.BoardService;
import br.com.comanda.util.VOToJSON;
import br.com.comanda.vos.Account;
import br.com.comanda.vos.Board;

public class NovaContaActivity extends Activity {
	static String[] mesas = { "Mesa 1", "Mesa2", "Mesa 3", "Mesa 4", "Mesa 5", "Mesa 6", "Mesa 7", "Mesa 8", "Mesa 9", "Mesa 10" };
	public ListView lstMesas;
	public Intent data = new Intent();

	private final BoardService boardService = new BoardService();
	private final AccountService accountService = new AccountService();

	public Board selectedBoard = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		setContentView(R.layout.nova_conta);
		// this.boardService.insertBoard();
		List<Board> boardList = this.boardService.readAllBoards();
		ArrayAdapter<Board> adapter = new BoardAdapter(this, R.layout.board_list, boardList);
		this.lstMesas = (ListView) findViewById(R.id.lstMesas);
		this.lstMesas.setAdapter(adapter);
		this.lstMesas.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		this.lstMesas.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {

				NovaContaActivity.this.data.putExtra("Valor", NovaContaActivity.this.lstMesas.getItemAtPosition(position).toString());
				NovaContaActivity.this.selectedBoard = (Board) NovaContaActivity.this.lstMesas.getItemAtPosition(position);

			}

		});

		Button btnConfirm = (Button) findViewById(R.id.btnConfirmaNovaConta);
		btnConfirm.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				EditText etClientName = (EditText) findViewById(R.id.etNomeCliente);

				String clientName = etClientName.getText().toString();
				NovaContaActivity.this.data.putExtra("cliente", clientName);

				Account account = new Account(null, clientName, NovaContaActivity.this.selectedBoard);
				account.setWaiter("garcom");

				Log.i("ACCOUNT JSON", VOToJSON.toAccountJSON(account).toString());

				NovaContaActivity.this.accountService.insertAccount(account);

				setResult(1, NovaContaActivity.this.data);
				finish();
			}
		});

		Button btnCancelar = (Button) findViewById(R.id.btnCancelarNovaConta);
		btnCancelar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
	}

	public List<Board> getBoards() {
		List<Board> boardList = new ArrayList<Board>();
		Board board = null;
		for (int i = 0; i < 5; i++) {
			board = new Board();
			board.setId(i + 1);
			board.setBoard("board " + 1);

			boardList.add(board);
		}

		return boardList;
	}

	@Override
	public void onBackPressed() {
		setResult(1, NovaContaActivity.this.data);
		super.onBackPressed();
	}

}
